package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.example.model.*;
import com.ls.entity.*;
import com.ls.mapper.ExampleMapper;
import com.ls.service.IExampleFieldService;
import com.ls.service.IExampleService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.ls.constant.SystemConstants.VALID_TRUE;

/**
 * <p>
 * 示例 服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-10-12
 */
@Service
@RequiredArgsConstructor
public class ExampleServiceImpl extends ServiceImpl<ExampleMapper, Example> implements IExampleService {
    private final IExampleFieldService exampleFieldService;

    /**
     * 新增示例
     */
    @Override
    public ResultData add(AddExampleModel model) {
        Example example = new Example();
        BeanUtils.copyProperties(model, example);
        example.setValidFlag(VALID_TRUE);
        SysUser sysUser = SaTokenUtilPy.getSysUser();
        example.setCreatorId(sysUser.getUserId());
        example.setUpdaterId(sysUser.getUserId());
        saveOrUpdate(example);
        if (model.getList() != null && model.getList().size() > 0) {
            for (AddExampleModel.info info : model.getList()) {
                ExampleField exampleField = new ExampleField();
                BeanUtils.copyProperties(info, exampleField);
                exampleField.setExampleId(example.getExampleId());
                exampleField.insert();
            }
        }
        return ResultData.success();
    }

    /**
     * 更新示例
     */
    @Override
    public ResultData edit(EditExampleModel model) {
        Example example = getById(model.getExampleId());
        if (example == null) {
            return ResultData.failure("示例不存在");
        }
        BeanUtils.copyProperties(model, example);
        SysUser sysUser = SaTokenUtilPy.getSysUser();
        example.setUpdaterId(sysUser.getUserId());
        exampleFieldService.remove(new LambdaQueryWrapper<ExampleField>().eq(ExampleField::getExampleId, model.getExampleId()));
        saveOrUpdate(example);
        if (model.getList() != null && model.getList().size() > 0) {
            for (EditExampleModel.info info : model.getList()) {
                ExampleField exampleField = new ExampleField();
                BeanUtils.copyProperties(info, exampleField);
                exampleField.setExampleId(example.getExampleId());
                exampleField.insert();
            }
        }
        return ResultData.success();

    }

    /**
     * 查看示例
     */
    @Override
    public ResultData pageList() {
        List<Example> list = list(new LambdaQueryWrapper<Example>()
        );
        List<ExampleVo> resultList = list.stream().map(example -> {
            ExampleVo exampleVo = new ExampleVo();
            BeanUtils.copyProperties(example, exampleVo);
            List<ExampleField> exampleFieldList = exampleFieldService.list(new LambdaQueryWrapper<ExampleField>()
                    .eq(ExampleField::getExampleId, example.getExampleId())
                    .orderByAsc(ExampleField::getFieldIndex)
            );
            List<ExampleVo.info> info = exampleFieldList.stream().map(exampleField -> {
                ExampleVo.info info1 = new ExampleVo.info();
                BeanUtils.copyProperties(exampleField, info1);
                return info1;
            }).collect(Collectors.toList());
            exampleVo.setList(info);
            return exampleVo;
        }).collect(Collectors.toList());

        return ResultData.successData(resultList);
    }
    @Override
    public ResultData getPaging(QueryExampleModel model) {
        List<Long> exampleList = list(new LambdaQueryWrapper<Example>()
                .eq(model.getValidFlag() != null, Example::getValidFlag, model.getValidFlag())
                .eq(model.getType() != null, Example::getType, model.getType())
        ).stream()
                .map(Example::getExampleId)
                .collect(Collectors.toList());
        List<ExampleField> exampleFieldList = exampleFieldService.list(new LambdaQueryWrapper<ExampleField>()
                .in(ExampleField::getExampleId,exampleList)
                .orderByAsc(ExampleField::getFieldIndex)
        );
        return ResultData.successData(exampleFieldList);
    }

    /**
     * 修改示例启用状态
     */
    @Override
    public ResultData updateStatus(updateStatusModel model) {
        Example example = getById(model.getExampleId());
        if (example == null) {
            return ResultData.failure("示例不存在");
        }
        BeanUtils.copyProperties(model, example);
        SysUser sysUser = SaTokenUtilPy.getSysUser();
        example.setUpdaterId(sysUser.getUserId());
        example.setUpdateTime(LocalDateTime.now());
        return updateById(example) ?
                ResultData.successMsg("修改成功") :
                ResultData.failure("修改失败");
    }

    /**
     * 删除示例
     */
    @Override
    public ResultData del(Long exampleId) {
        Example example = getById(exampleId);
        if (example == null) {
            return ResultData.failure("示例不存在");
        }
        exampleFieldService.remove(new LambdaQueryWrapper<ExampleField>().eq(ExampleField::getExampleId, exampleId));
        return example.deleteById(exampleId) ? ResultData.successMsg("删除成功") : ResultData.failure("删除失败");
    }
}
